package com.sk.cardeal.utils;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.sk.cardeal.callback.MyCallBack;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by sk on 2018/1/10.
 */

public class UpLoadUtils {

    public static void uploadPicTask(String path, final MyCallBack callBack) {

        String imageObjectKey;
        Date date = new Date();
        imageObjectKey = new SimpleDateFormat("yyyyMMddssSSS").format(date) + ".jpg";
        PutObjectRequest put = new PutObjectRequest(HttpAddress.bucketName, "images/repairPic/" + imageObjectKey, path);
        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
            }
        });

        OSSAsyncTask task = MyApplicationLike.getOss().asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
                    @Override
                    public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                        callBack.success(request.getObjectKey());
                    }
                    @Override
                    public void onFailure(PutObjectRequest request, ClientException
                            clientExcepion, ServiceException serviceException) {
                        // 请求异常
                        if (clientExcepion != null) {

                            clientExcepion.printStackTrace();
                        }
                        if (serviceException != null) {


                        }
                    }
                }
        );
    }

}
